package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.admin.service.ScheduleService;
import com.his.dao.domain.Do.SelectAllScheduleDo;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.dto.ScheduleDto;
import com.his.dao.domain.dto.SelectAllScheduleDto;
import com.his.dao.domain.entity.Schedule;
import com.his.dao.mapper.ScheduleMapper;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import com.his.util.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {
    private ScheduleMapper scheduleMapper;

    @Override
    public RestResult all(ListDto listDto) {
        SelectAllScheduleDto selectAllScheduleDto = MapUtil.conversionMapToObj((Map) listDto.getObj(), SelectAllScheduleDto.class);

        String[] date = selectAllScheduleDto.getStartTime().split("-");

        String endTime = String.format("%s-%s", StringUtil.calculateDate(date[0], date[1]), date[2]);

        List<SelectAllScheduleDo> selectAllScheduleDoList = scheduleMapper.selectAll(
                selectAllScheduleDto.getHospitalId(),
                selectAllScheduleDto.getSectionId(),
                selectAllScheduleDto.getStartTime(),
                endTime);

        List<SelectAllScheduleDo> scheduleDos = new ArrayList<>();

        for (int i = 0; i < 31; i++) {
            scheduleDos.add(SelectAllScheduleDo
                    .builder()
                    .userId(0)
                    .oldId(0)
                    .build());
        }

        for (SelectAllScheduleDo selectAllScheduleDo : selectAllScheduleDoList) {
            scheduleDos.set(selectAllScheduleDo.getDate(), selectAllScheduleDo);
        }

        return new RestResultBuilder().success(scheduleDos);
    }

    @Override
    public RestResult schedule(ScheduleDto scheduleDto) {
        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<Schedule>()
                .eq("user_id", scheduleDto.getOldId())
                .eq("date", scheduleDto.getDate());
        Schedule schedule = scheduleMapper.selectOne(queryWrapper);

        if (schedule != null) {
            int id = schedule.getScheduleId();
            schedule = Schedule
                    .builder()
                    .scheduleId(id)
                    .userId(scheduleDto.getUserId())
                    .date(schedule.getDate())
                    .updateTime(LocalDateTime.now())
                    .build();
            updateById(schedule);

            return new RestResultBuilder().success();
        }

        schedule = Schedule
                .builder()
                .userId(scheduleDto.getUserId())
                .date(LocalDate.parse(scheduleDto.getDate()))
                .createTime(LocalDateTime.now())
                .build();

        save(schedule);

        return new RestResultBuilder().success();
    }
}
